package com.miyake.client.tools;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UploadDialog extends DialogBox {
	FileUpload fileUpload;
	
	public String getText() {		
		String fname = fileUpload.getFilename();
		String[] fs = fname.split("\\\\");
		return fs[fs.length - 1];
	}
	
	public UploadDialog(final String key) {
		setHTML("Upload");
		VerticalPanel p = new VerticalPanel();
		this.setWidget(p);
		p.setWidth("259px");
		
		final FormPanel formPanel = new FormPanel();
		p.add(formPanel);
		formPanel.setSize("100%", "23px");
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		fileUpload = new FileUpload();	
		fileUpload.setName("fileupload");
		formPanel.setWidget(fileUpload);
		

		formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				hide(false);
			}
		});
		
		HorizontalPanel h = new HorizontalPanel();
		p.add(h);
		Button button = new Button("OK");
		h.add(button);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				formPanel.setAction("/fileServlet?name=" + getText() + "&key=" + key);
				formPanel.submit();
			}
		});
		
		Button cancel = new Button("Cancel");
		h.add(cancel);
		cancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide(true);	
			}
		});
	}
}
